﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using System.Device.Location;

namespace ChiantiPhoneListApp
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();
            this.DealMap.Mode = new RoadMode();
            
        }

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedIndex = "";

            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                ChiantiServiceReference5.Deal deal = null;
                ChiantiServiceReference5.Business business = null;

                int index = int.Parse(selectedIndex);
                
                deal = App.ViewModel.Deals[index];
                business = App.ViewModel.GetBusinessByDeal(deal.ID);

                this.PageTitle.DataContext = business;
                this.DataContext = deal;
                
                
                GeoCoordinate coordinate = new GeoCoordinate(business.Latitude,-1*business.Longitude);
                MapLayer layer = new MapLayer();
                Pushpin pushpin = new Pushpin();
                pushpin.Location = coordinate;
                pushpin.Name = deal.Title;

                MapPolygon polygon = new MapPolygon();
                polygon.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
                polygon.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Yellow);
                polygon.StrokeThickness = 5;
                polygon.Opacity = 0.7;
                polygon.Locations = new LocationCollection();
                polygon.Locations.Add(coordinate);
                
                ScaleTransform st = new ScaleTransform();               
                st.ScaleX = 0.25;                
                st.ScaleY = 0.25;
                st.CenterX = (pushpin as FrameworkElement).Height / 2;
                st.CenterY = (pushpin as FrameworkElement).Height / 2;

                pushpin.RenderTransform = st;
                pushpin.Background = new SolidColorBrush(Colors.Blue);
                pushpin.Height = 500;
                pushpin.Width = 500;

                polygon.RenderTransform = st;
               
                this.DealMap.Children.Add(polygon);
               
                this.DealMap.SetView(coordinate, 15);
            }
        }
    }
}